package com.lp.yuojbackendjudgeservice.judge.codesandbox.impl;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.lp.yuojbackendcommon.common.ErrorCode;
import com.lp.yuojbackendcommon.exception.BusinessException;
import com.lp.yuojbackendjudgeservice.judge.codesandbox.CodeSandbox;
import com.lp.yuojbackendmodel.codesrandbox.ExecutedCodeRequest;
import com.lp.yuojbackendmodel.codesrandbox.ExecutedCodeResponse;
import org.apache.commons.lang3.StringUtils;


/**
 * 远程代码沙箱 (调用自动的远程代码沙箱)
 */
public class RemoteCodeSandbox implements CodeSandbox {
    public static final String AUTH_REQUEST_HEADER ="auth";
    public static final String AUTH_REQUEST_SECRET="secretKey";
    @Override
    public ExecutedCodeResponse executeCode(ExecutedCodeRequest executedCodeRequest) {
        String jsonStr = JSONUtil.toJsonStr(executedCodeRequest);
        //String url = "http://192.168.32.131:8090/executeCode";
        String url = "http://localhost:8090/executeCode";
        String executeCodeResponse = HttpUtil.createPost(url)
                .body(jsonStr)
                .header(AUTH_REQUEST_HEADER,AUTH_REQUEST_SECRET)
                .execute()
                .body();
        if (StringUtils.isBlank(executeCodeResponse)){
            throw new BusinessException(ErrorCode.API_RESPONSE_ERROR,"远程接口请求错误");
        }
        return JSONUtil.toBean(executeCodeResponse, ExecutedCodeResponse.class);
    }
}
